package dongbai.soft.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import dongbai.soft.entity.ProductEntity;
import dongbai.soft.entity.SupplierEntity;
import dongbai.soft.feignclient.DispatchClient;
import dongbai.soft.feignclient.FinanceClient;
import dongbai.soft.service.ProductService;
import dongbai.soft.service.SupplierService;
import dongbai.soft.utils.Constant;
import dongbai.soft.utils.PageUtils;
import dongbai.soft.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;


/**
 * @author SingDeer
 * @email
 * @date 2023-06-26 14:01:21
 */
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    @Autowired
    private SupplierService supplierService;

    @Autowired
    private FinanceClient financeClient;

    @Autowired
    private DispatchClient dispatchClient;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions(":product:list")
    public R list(@RequestBody Map<String, Object> params) {
        PageUtils page = productService.queryPage(params);
        long sum = (int) productService.count(new QueryWrapper<ProductEntity>().apply("wNum>num"));
        return Objects.requireNonNull(R.ok().put("page", page)).put("sum", sum);
    }


    /**
     * 新建
     */
    @RequestMapping("/new")
    public R save(@RequestBody Map<String, String> maps) {
        if (productService.getOne(new QueryWrapper<ProductEntity>().eq("name", maps.get("name")).eq("status", Constant.STATE_Y)) != null) {
            return R.error(Constant.PRO_DUP);
        }
        ProductEntity productEntity = new ProductEntity(maps);
        SupplierEntity supplierEntity = supplierService.getById(maps.get("SID"));
        List<String> pids = new ArrayList<>(Arrays.asList(supplierEntity.getPidlist().split(", ")));
        if (pids.get(0).equals("")) {
            pids = new ArrayList<>();
        }
        pids.add(productEntity.getPid());
        supplierEntity.setPidlist(pids.toString().substring(1, pids.toString().length() - 1));
        supplierService.updateById(supplierEntity);
        productService.save(productEntity);
        return R.ok();
    }

    /**
     * 修改商品信息
     */
    @RequestMapping("/editinfo")
    //@RequiresPermissions(":product:update")
    public R editinfo(@RequestBody Map<String, Object> maps) {
        if (productService.getOne(new QueryWrapper<ProductEntity>().eq("name", maps.get("name")).eq("status", Constant.STATE_Y)) != null) {
            return R.error(Constant.PRO_DUP);
        }
        productService.update(new UpdateWrapper<ProductEntity>().eq("PID", maps.get("PID"))
                .set("name", maps.get("name")).set("price", maps.get("price"))
                .set("TID", maps.get("TID")).set("wNum", maps.get("wNum")));

        return R.ok();
    }

    /*
     * 修改商品数量
     * 0进货-生成进货单-调度提醒
     * 1退货给供应商 -生成货单
     * 2商品失效-生成亏损单
     * 3签收分站退货-不生成货单-调度提醒
     * 4审批后预定货物-不需要生成货单-订单挂起
     * 5调度中心修改挂起订单后，变动商品数量
     */
    @RequestMapping("/editnum")
    public R editnum(@RequestBody Map<String, Object> maps) {
        int status = (int) maps.get("status");
        int num = (int) maps.get("num");
        ProductEntity productEntity = productService.getOne(new QueryWrapper<ProductEntity>().eq("PID", maps.get("PID")));

        //0123货物可以直接改变数量，012需要生成货单，03需要通知调度查看挂起
        if (status < 4) {
            productEntity.setNum(productEntity.getNum() + num);
            productService.updateById(productEntity);
            if (status < 3) {
                maps.put("SID", productEntity.getSid());
                financeClient.newgoodorder(maps);
            }
            if (status == 0 || status == 3) {
                Map<String, Object> pmap = new HashMap<>();
                pmap.put("PID", maps.get("PID"));
                pmap.put("num", productEntity.getNum());
                dispatchClient.eSOrder(pmap);
            }
            //货物足够就直接减少货物，不足则不减少，分别需要通知调度中心将订单置为待发货和挂起状态
        } else if (status == 4) {
            Map<String, Object> smap = new HashMap<>();
            smap.put("isend", Constant.ORUN);
            smap.put("OID", maps.get("OID"));
            if (productEntity.getNum() + num < 0) {
                smap.put("status", 0);
                smap.put("PID", maps.get("PID"));
            } else {
                smap.put("status", 2);
                productEntity.setNum(productEntity.getNum() + num);
                productService.updateById(productEntity);
            }
            dispatchClient.setStatus(smap);
            //调度中心将挂起订单激活，预定货物
        } else {
            productEntity.setNum(num);
            productService.updateById(productEntity);
        }

        return R.ok();
    }


    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Map<String, String> maps) {
        productService.update(new UpdateWrapper<ProductEntity>().eq("PID", maps.get("PID"))
                .set("status", Constant.STATE_N));
        return R.ok();
    }

    /**
     * 查看单个商品的信息
     */
    @RequestMapping("/info")
    public R info(@RequestBody Map<String, String> maps) {
        return R.ok().put("data", productService.getById(maps.get("PID")));
    }

    /**
     * 该商品需要手动操作入库
     */
    @RequestMapping("/manual")
    public boolean manual(@RequestBody String PID) {
        return productService.getById(PID).getStatus();
    }


}
